package bean
{
	import define.DBConst;
	
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.SQLEvent;
	
	import orm.armory.vo.TblBossVO;
	import orm.armory.vo.TblItemPropertyVO;
	import orm.armory.vo.TblItemVO;
	
	public class ItemPropertyBean extends EventDispatcher
	{
		// Drop: Zone - Boss(DownType)
		// Vendor: Token * Cost
		public var postAcquisition:String;
		public var postAcquisitionComment:String;
		
		private var _postAcquisitionItemID:String;
		
		private var _itemVo:TblItemVO;
		private var _tokenItemVo:TblItemVO;
		private var _itemPropertyVo:TblItemPropertyVO;
		
		private var _loadingStep:int = -1;
		private var _loadingFunctions:Array;
		public static const EVENT_LOADING_COMPLETED:String = "beanEventLoadingCompleted";
		
		public function set itemVo(data:TblItemVO):void
		{
			_itemVo = data;
			
			_loadingFunctions = [loadTokenItem, loadDropInfo, loadVendorInfo, loadItemProperty];
			loadNext();
		}
		public function get itemVo():TblItemVO
		{
			return _itemVo;
		}
		public function get itemPropertyVo():TblItemPropertyVO
		{
			return _itemPropertyVo;
		}
		private function loadNext():void
		{
			_loadingStep++;

			// COLLECT_FINISH, COLLECT_ERROR
			if(_loadingStep > _loadingFunctions.length - 1)
			{
//				trace(this.postAcquisition + " -> " + _itemPropertyVo.info_item_name);
//				if(postAcquisitionComment != null)
//					trace("\t" + this.postAcquisitionComment);
				this.dispatchEvent(new Event(EVENT_LOADING_COMPLETED));
			}
			// run
			else
			{
				(_loadingFunctions[_loadingStep] as Function).call(this);
			}
		}
		private function loadTokenItem():void
		{
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = DBConst.armoryConn;
			var sql:String = "select * from tblitem token "
				+ "where token.id = " 
				+ "(select tbl_item_id from ritemtoken "
				+ "where tbl_item_id_token = @tbl_item_id_token) ";
			stmt.text = sql;
			stmt.parameters["@tbl_item_id_token"] = _itemVo.id;
			stmt.itemClass = TblItemVO;
			
			stmt.addEventListener(SQLEvent.RESULT, loadTokenItemHandler);
			stmt.execute();
		}
		private function loadTokenItemHandler(event:SQLEvent):void
		{			
			var r:SQLResult = (event.target as SQLStatement).getResult();
			if(r.data != null)
			{
				_tokenItemVo = r.data[0];
				_postAcquisitionItemID = _tokenItemVo.item_id;
			}
			else
			{
				_postAcquisitionItemID = _itemVo.item_id;
			}
			
			loadNext();
		}
		private function loadDropInfo():void
		{
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = DBConst.armoryConn;
			var sql:String = "select tblboss.* "
				+ "from tblitem, tblboss, rbossitem "
				+ "where tblitem.item_id = @item_id "
				+ "and tblitem.id = rbossitem.tbl_item_id "
				+ "and tblboss.id = rbossitem.tbl_boss_id ";
			stmt.text = sql;
			stmt.parameters["@item_id"] = _postAcquisitionItemID;
			stmt.itemClass = TblBossVO;
			
			stmt.addEventListener(SQLEvent.RESULT, loadDropInfoHandler);
			stmt.execute();
		}
		private function loadDropInfoHandler(event:SQLEvent):void
		{			
			var r:SQLResult = (event.target as SQLStatement).getResult();
			for each(var vo:TblBossVO in r.data)
				addToAcquisition(vo.zone_name + " - " + vo.boss_name + " (" + vo.down_type + ")");
				
			loadNext();
		}
		
		private function loadVendorInfo():void
		{
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = DBConst.armoryConn;
			var sql:String = "select tblvendor.vendor_type, rvendorsell.cost, tblvendor.comment " 
				+ "from tblitem, tblvendor, rvendorsell "
				+ "where tblitem.item_id = @item_id "
				+ "and tblitem.id = rvendorsell.tbl_item_id "
				+ "and tblvendor.id = rvendorsell.tbl_vendor_id ";
			stmt.text = sql;
			stmt.parameters["@item_id"] = _postAcquisitionItemID;
			
			stmt.addEventListener(SQLEvent.RESULT, loadVendorInfoHandler);
			stmt.execute();
		}
		private function loadVendorInfoHandler(event:SQLEvent):void
		{			
			var r:SQLResult = (event.target as SQLStatement).getResult();
			for each(var vo:Object in r.data)
			{
				if(vo.cost == null)
					addToAcquisition(vo.vendor_type);
				else
					addToAcquisition(vo.vendor_type + " * " + vo.cost);
				
				if(vo.comment != null)
					this.postAcquisitionComment = (vo.comment as String).replace(/\s\|\|\s/g, "\n");
			}
				
			loadNext();
		}
		
		private function addToAcquisition(s:String):void
		{
			if(this.postAcquisition == null)
				this.postAcquisition = s;
			else
				this.postAcquisition = this.postAcquisition + "\n" + s;
		}
		
		private function loadItemProperty():void
		{
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = DBConst.armoryConn;
			stmt.text = "select * from TblItemProperty where info_item_id = @item_id ";
			stmt.parameters["@item_id"] = _itemVo.item_id;
			stmt.itemClass = TblItemPropertyVO;
			
			stmt.addEventListener(SQLEvent.RESULT, loadItemPropertyHandler);
			stmt.execute();
		}
		private function loadItemPropertyHandler(event:SQLEvent):void
		{
			var r:SQLResult = (event.target as SQLStatement).getResult();
			_itemPropertyVo = r.data[0];
			
			loadNext();
		}
	}
}